import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
const routes = [
  {
    path: "/",
    component: () => import("@/view/home/home.vue"),
    hidden: true,
    meta: { id: 0 },
    beforeEnter: (to, from) => {
      localStorage.setItem("routerid", to.meta.id);
    },
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/view/home/home.vue"),
    hidden: true,
    meta: { id: 0 },
  },
  {
    path: "/Professional",
    name: "Professional",
    component: () => import("@/view/Professional/Professional.vue"),
    hidden: true,
    meta: { id: 1 },
  },
  {
    path: "/Course",
    name: "Course",
    component: () => import("@/view/Course/Course.vue"),
    hidden: true,
    meta: { id: 2 },
  },
  {
    path: "/Microcourse",
    name: "Microcourse",
    component: () => import("@/view/Microcourse/Microcourse.vue"),
    hidden: true,
    meta: { id: 3 },
  },
  {
    path: "/Skill",
    name: "Skill",
    component: () => import("@/view/Skill/Skill.vue"),
    hidden: true,
    meta: { id: 5 },
  },
  {
    path: "/Training",
    name: "Training",
    component: () => import("@/view/Training/Training.vue"),
    hidden: true,
    meta: { id: 4 },
  },
  {
    path: "/Material",
    name: "Material",
    component: () => import("@/view/Material/Material.vue"),
    hidden: true,
    meta: { id: 6 },
  },
  {
    path: "/Exams",
    name: "Exams",
    component: () => import("@/view/Exams/Exams.vue"),
    hidden: true,
    meta: { id: 7, name: "Exams" },
    children: [
      {
        path: "/Exams",
        name: "examsList",
        component: () => import("@/view/Exams/components/examsList.vue"),
        hidden: true,
        meta: { id: 7, name: "Exams" },
      },
      {
        path: "/test",
        name: "test",
        component: () => import("@/view/Exams/components/test.vue"),
        hidden: true,
        meta: { id: 7, name: "Exams" },
      },
      {
        path: "/examResults",
        name: "examResults",
        component: () => import("@/view/Exams/components/examResults.vue"),
        hidden: true,
        meta: { id: 7, name: "Exams" },
      },
    ],
  },
  {
    path: "/Space",
    name: "Space",
    component: () => import("@/view/Space/Space.vue"),
    hidden: true,
    meta: { id: 8, name: "Space" },
    children: [
      {
        path: "/Space",
        name: "spaceList",
        component: () => import("@/view/Space/components/spaceList.vue"),
        hidden: true,
        meta: { id: 8, name: "Space" },
      },
      {
        path: "/release",
        name: "release",
        component: () => import("@/view/Space/components/release.vue"),
        hidden: true,
        meta: { id: 8, name: "Space" },
      },
      {
        path: "/spaceDtaild",
        name: "spaceDtaild",
        component: () => import("@/view/Space/components/spaceDtaild.vue"),
        hidden: true,
        meta: { id: 8, name: "Space" },
      },
    ],
  },
  {
    path: "/UserCenter",
    name: "UserCenter",
    component: () => import("@/view/UserCenter/index.vue"),
    hidden: true,
  },
  {
    path: "/questionnaire",
    name: "questionnaire",
    component: () => import("@/view/questionnaire/questionnaire.vue"),
    hidden: true,
  },
  {
    path: "/historyList",
    name: "historyList",
    component: () => import("@/view/historyList/historyList.vue"),
    hidden: true,
  }
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  localStorage.setItem("routerid", to.meta.id);
  if (to.meta.name == "Space" || to.meta.name == "Exams") {
    if (localStorage.getItem("username")) {
      next();
    } else {
      ElMessage.error("请先登录");
      localStorage.setItem("routerid", 0);
      next({
        path: "/home",
      });

      return;
    }
  } else {
    next();
  }
});
export default router;
